home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <gl.h>
- #include <math.h>
- #include <stdio.h>
- #include <malloc.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include "defines.h"
- #include "control.h"
- #include "data.h"
- #include "file.h"
-
- extern struct node_struct *root, *selected_patch;
-
-
- static void number_nodes(struct node_struct *object, int *counter)
- {
- if (object != NULL)
- {
- object->node_id = *counter;
-
- (*counter)++;
-
- number_nodes(object->child, counter);
- number_nodes(object->sibling, counter);
- }
- }
-
-
- static write_nodes(struct node_struct *object, FILE *fp)
- {
- int i,j,k;
-
- if (object != NULL)
- {
- fprintf(fp, "%d %d\n",object->node_id, object->node_type);
-
- if (object->parent == NULL)
- fprintf(fp, "-1 ");
- else
- fprintf(fp, "%d ", object->parent->node_id);
-
- if (object->child == NULL)
- fprintf(fp, "-1 ");
- else
- fprintf(fp, "%d ", object->child->node_id);
-
- if (object->sibling == NULL)
- fprintf(fp, "-1 ");
- else
- fprintf(fp, "%d ", object->sibling->node_id);
-
- if (object->prev_sibling == NULL)
- fprintf(fp, "-1\n");
- else
- fprintf(fp, "%d\n", object->prev_sibling->node_id);
-
-
- if (object->node_type == PATCH)
- {
- for (i = 0; i< 4; i++)
- for (j = 0; j < 4; j++)
- fprintf(fp, "%f %f %f\n",
- object->patch->control[i][j][0],
- object->patch->control[i][j][1],
- object->patch->control[i][j][2]);
-
- for (i = 0; i<4; i++)
- {
- fprintf(fp, "%d ", object->patch->zipper[i]->zipped);
- fprintf(fp, "%d ", object->patch->zipper[i]->zip_type);
-
- if (object->patch->zipper[i]->patch == NULL)
- fprintf(fp, "-1 ");
- else
- fprintf(fp, "%d ", object->patch->zipper[i]->patch->node_id);
-
- fprintf(fp, "%d\n", object->patch->zipper[i]->edge);
-
- fprintf(fp, "%d %d %d %d\n",
- object->patch->zipper[i]->s_index,
- object->patch->zipper[i]->s_step,
- object->patch->zipper[i]->t_index,
- object->patch->zipper[i]->t_step);
-
- fprintf(fp, "%d %d %d %d\n",
- object->patch->zipper[i]->i_s_index,
- object->patch->zipper[i]->i_s_step,
- object->patch->zipper[i]->i_t_index,
- object->patch->zipper[i]->i_t_step);
- }
- }
-
- fprintf(fp, "\n");
-
- write_nodes(object->child,fp);
- write_nodes(object->sibling,fp);
- }
- }
-
-
- static void write_file(char *filename, int count)
- {
- FILE *fp;
-
- if ( (fp = fopen( filename, "w" )) == NULL )
- {
- fprintf(stderr,"Can't open file for writing\n");
- }
- else
- {
- fprintf(fp,"%d\n",count);
- write_nodes(root, fp);
- fclose(fp);
-
- }
- }
-
-
- void save_file(void)
- {
- int node_count = 0;
-
- number_nodes(root, &node_count);
-
- if (node_count == 1)
- fprintf(stderr, "Nothing to save\n");
- else
- write_file("/tmp/junk.snb", node_count);
- }
-
- #define MAX_OBJECTS 100
- struct node_struct *new_root[MAX_OBJECTS];
-
- static void fix(Coord n[3])
- {
- /*
- if ((n[0] < .1) && (n[0] > -.1))
- n[0] = 0.0;
- else if (n[0] > 0.0)
- {
- if (n[0] <.3)
- n[0] = .276;
- else
- n[0] = .5;
- }
- else
- {
- if (n[0] >-.3)
- n[0] = -.276;
- else
- n[0] = -.5;
- }
-
- if ((n[2] < .1) && (n[2] > -.1))
- n[2] = 0.0;
- else if (n[2] > 0.0)
- {
- if (n[2] <.3)
- n[2] = .276;
- else
- n[2] = .5;
- }
- else
- {
- if (n[2] >-.3)
- n[2] = -.276;
- else
- n[2] = -.5;
- }
- */
-
-
-
- }
-
-
-
-
-
-
- void read_file(char *filename)
- {
- FILE *fp;
- int count,i,j,k,a,index;
- struct node_struct *object;
- struct patch_struct *new_patch;
-
- if ( (fp = fopen( filename, "r" )) == NULL )
- {
- fprintf(stderr,"Can't open file for reading\n");
- }
- else
- {
- fscanf(fp,"%d\n",&count);
-
- for (a=0; a<count; a++)
- new_root[a] = make_new_node();
-
- for (a=0; a<count; a++)
- {
- object = new_root[a];
-
- fscanf(fp, "%d %d\n", &object->node_id, &object->node_type);
-
- fscanf(fp, "%d ", &index);
- object->parent = new_root[index];
-
- fscanf(fp, "%d ", &index);
- object->child = new_root[index];
-
- fscanf(fp, "%d ", &index);
- object->sibling = new_root[index];
-
- fscanf(fp, "%d\n", &index);
- object->prev_sibling = new_root[index];
-
- if (object->node_type == PATCH)
- {
- new_patch = make_new_patch();
- object->patch = new_patch;
-
- for (i = 0; i< 4; i++)
- object->patch->zipper[i] = make_new_zipper();
-
-
- for (i = 0; i< 4; i++)
- for (j = 0; j < 4; j++)
- {
- fscanf(fp, "%f %f %f\n",
- &object->patch->control[i][j][0],
- &object->patch->control[i][j][1],
- &object->patch->control[i][j][2]);
-
- /* For hacky development use only */
- fix(object->patch->control[i][j]);
- }
-
- for (i = 0; i<4; i++)
- {
- fscanf(fp, "%d ", &object->patch->zipper[i]->zipped);
- fscanf(fp, "%d ", &object->patch->zipper[i]->zip_type);
-
- fscanf(fp, "%d ", &index);
- object->patch->zipper[i]->patch = new_root[index];
-
- fscanf(fp, "%d\n", &object->patch->zipper[i]->edge);
-
- fscanf(fp, "%d %d %d %d\n",
- &object->patch->zipper[i]->s_index,
- &object->patch->zipper[i]->s_step,
- &object->patch->zipper[i]->t_index,
- &object->patch->zipper[i]->t_step);
-
- fscanf(fp, "%d %d %d %d\n",
- &object->patch->zipper[i]->i_s_index,
- &object->patch->zipper[i]->i_s_step,
- &object->patch->zipper[i]->i_t_index,
- &object->patch->zipper[i]->i_t_step);
- }
- }
- }
- fclose(fp);
- new_root[0]->node_type = OBJECT;
- attach_node_to_parent(new_root[0],root);
- }
- }
-
- void load_file(void)
- {
- read_file("/tmp/junk.snb");
- draw_display();
- }
-
-